// cd /projects/hsieh_project/proj_201809/code_1_data/
// qstata cw_geo_msa_czone.do &

set linesize 255
capture log close

clear all
cd /projects/

di "Started at $S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_datac "/projects/data_commons/"
global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_data "${dir_proj}/data/"

//==============================================================================
// Alternative msa_czone crosswalks

//------------------------------------------------------------------------------
// Assign czone to rural MSA
// Now in data_commons/


use ${dir_datac}/cw/cw_geo_fips_msa, clear
merge 1:1 fips using ${dir_datac}/cw/cw_geo_fips_czone

gen msa_czone = msa
/*REDACTED
A change to msa_czone is redacted */

tab msa_str if msa_czone == .

keep fips msa_czone
drop if msa_czone == .
sort fips
recast long msa
compress

saveold ${dir_data}/cw_geo_fips_msa_czone_v12, replace v(12)


//------------------------------------------------------------------------------
// Assign MSA fips to czone

use ${dir_datac}/cw/cw_geo_fips_msa, clear
merge 1:1 fips using ${dir_datac}/cw/cw_geo_fips_czone
drop if _merge == 2
drop if _merge == 1 & msa < 60 & msa != 40

sort czone msa
by czone: egen in_msa = total(msa >= 60)
gen czone_in_msa = 1 if in_msa > 0
gen fips_in_msa = 1 if msa >= 60

keep fips czone fips_in_msa czone_in_msa
order fips czone fips_in_msa czone_in_msa
compress

saveold ${dir_data}/cw_geo_fips_czone_in_msa_v12, replace v(12)

di "Ended at $S_DATE $S_TIME"
capture log close
// End of do file
